;		MIDI_Test.ASM            		       25/01/12
;**********************************************************************
;**********************************************************************
;                                                                     *
;    Filename:	    MIDI_Test.ASM                                     *
;    Date:          25/01/12				                          *
;    File Version:  V 1.0                                             *
;                                                                     *
;    Author:        Jeffry Miller                                     *
;    Company:       Nova Scotia Community College - Kingstec          *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:  Let user know what other files are necessary    *
;                     p16F887.inc                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:  Testing sending note on and note off MIDI signals        *
;			 with the USART                                           *
;**********************************************************************


  LIST p=16f887		          	  ; list directive to define processor
  INCLUDE <p16f887.inc> 	      ; processor specific variable definitions

;**********************************************************************
		__CONFIG	 _CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF & _DEBUG_OFF
		
		__CONFIG	 _CONFIG2, _BOR40V & _WRT_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
;********************************************************************** 
;********************** Header **********************************************
;**************** DEFINING VARIABLES ****************************************

dataL	EQU 0x20				   ; General purpose registers
       cblock      0x21            ; Block of variables starts at address 20h
       w_temp                      ; Variable at address 21h
       pclath_temp                 ; Variable at address 22h
       status_temp                 ; Variable at address 23h
       endc      
       
;************************ START OF PROGRAM **********************************
       org         0x0000          ; Address of the first program instruction
       goto        main            ; Go to label "main"
       
;************************ INTERRUPT ROUTINE *********************************
       org         0x0004          ; Interrupt vector
       movwf       w_temp          ; Saves value in register W
       movf        STATUS          ; Saves value in register STATUS
       movwf       status_temp
       movf        PCLATH          ; Saves value in register PCLATH
       movwf       pclath_temp
       
       
       movf        pclath_temp,w   ; PCLATH is given its original content
       movwf       PCLATH
       movf        status_temp,w   ; STATUS is given its original content
       movwf       STATUS
       swapf       w_temp,f        ; W is given its original content
       swapf       w_temp,w
       retfie                      ; Return from interrupt routine
       
;************************ MAIN PROGRAM **************************************
main                               ; Start of the main program

       banksel     ANSEL           ; Bank containing register ANSEL
       clrf        ANSEL           ; Clears registers ANSEL and ANSELH
       clrf        ANSELH          ; All pins are digital
       
       banksel     TRISB           ; Selects bank containing register TRISB
	   movlw	   b'00000001'	   ; Set bit 0 to input, all others are outputs
	   movwf	   TRISB
       clrf		   TRISC		   ; Ensure TX pin is output

       banksel	   SPBRG
	   movlw	   d'9'			   ; Baudrate = 20000000/(64*(9+1)) = 31 250
	   movwf	   SPBRG
	   bsf		   TXSTA,5		   ; Enable serial transmission 
     banksel	 RCSTA
	   bsf		   RCSTA,7		   ; Enable the serial port

	   call		   delay		   ; Wait until setup finishes

press_loop
	   banksel	   PORTB
	   btfsc	   PORTB,0		   ; Check for Button press
		   goto	   press_loop
	   call		   xmit
	   goto		   press_loop
;****************************************************************************************
; Subroutine to provide some settling time for start up
;****************************************************************************************	
delay
			banksel	dataL
			clrf	dataL

settle		decfsz	dataL,f
					goto settle
			
			movf	RCREG,W
			movf	RCREG,W
			movf	RCREG,W

			return
;****************************************************************************************
; Transmit the Midi note A4 on a short delay then, A4 off via the USART
;****************************************************************************************
xmit
		   movlw	b'10010000'		; Note on, channel 0
		   call		transmit
		   movlw	b'01000101'		; A4
		   call		transmit
		   movlw	b'01000000'		; Medium velocity
		   call		transmit
		   call		delay
		   movlw	b'10000000'		; Note off, channel 0
		   call		transmit
		   movlw	b'01000101'		; A4
		   call		transmit
		   movlw	b'01000000'		; Medium velocity
		   call		transmit
		   call		delay
		   banksel PORTB
		   return			
;****************************************************************************************
; Transmit the character that is on WREG to RS232 and wait until the sending is complete
;****************************************************************************************

transmit
			banksel	TXREG
			movwf	TXREG

			banksel TXSTA
waittrn		btfss	TXSTA,TRMT
					goto waittrn
			return
;****************************************************************************************

       end                         ; End of program